package com.sunshine.monitor.jvm;

import java.lang.management.GarbageCollectorMXBean;
import java.lang.management.ManagementFactory;
import java.util.List;

public class JVMGC implements IJVMGC {

	private static final JVMGC jvmGc = new JVMGC();

	private GarbageCollectorMXBean youngGC;

	private GarbageCollectorMXBean fullGC;

	public static IJVMGC getInstance() {
		return jvmGc;
	}

	private JVMGC() {

		List<GarbageCollectorMXBean> lists = ManagementFactory
				.getGarbageCollectorMXBeans();
		for (GarbageCollectorMXBean t : lists) {
			if ("Copy".equals(t.getName())) {
				youngGC = t;
			} else if ("MarkSweepCompact".equals(t.getName())) {
				fullGC = t;
			}
		}

	}

	public long getYoungGCCollectionCount() {
		return youngGC.getCollectionCount();
	}

	public long getYoungGCCollectionTime() {
		return youngGC.getCollectionTime();
	}

	public long getFullGCCollectionCount() {
		return fullGC.getCollectionCount();
	}

	public long getFullGCCollectionTime() {
		return fullGC.getCollectionTime();
	}
}
